package com.jy.app.lib.view.custom;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.jy.app.lib.R;


/**
 * Universal Loading View , it
 * Created by aliouswang on 15/5/6.
 */
public class UniversalLoadingView extends ViewGroup{

    private MaterialCircleView materialCircleView;

    private int sWidth;
    private int sHeight;
    private int radius;

    public UniversalLoadingView(Context context) {
        this(context, null);
    }

    public UniversalLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UniversalLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray t = null;
        try {
            t = context.obtainStyledAttributes(attrs, R.styleable.MaterialCircleView,
                    0, defStyleAttr);
            radius = t.getDimensionPixelSize(R.styleable.MaterialCircleView_radius,
                    MaterialCircleView.dpToPx(50, getResources()));
        } finally {
            if (t != null) {
                t.recycle();
            }
        }

        materialCircleView = new MaterialCircleView(context, attrs, defStyleAttr);
        addView(materialCircleView);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        LayoutParams params = (LayoutParams) materialCircleView.getLayoutParams();
        sWidth = MeasureSpec.getSize(widthMeasureSpec);
        sHeight = MeasureSpec.getSize(heightMeasureSpec);
        params.left = 0;
        params.top = 0;
        params.width = radius;
        params.height = radius;


        setMeasuredDimension(sWidth, sHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        LayoutParams params = (LayoutParams) materialCircleView.getLayoutParams();
        materialCircleView.layout(params.left, params.top, params.left + params.width
                    , params.top + params.height);
    }

    public void setBGradient(boolean isBGradient){
        materialCircleView.setbGradient(isBGradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(
            AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(
            ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

    private static class LayoutParams extends ViewGroup.LayoutParams {

        public int left = 0;
        public int top = 0;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }
    }
}
